/*----------------------------------------------------------*/
/* XMLHTTPRequest Enable                                    */
/*----------------------------------------------------------*/
function createObject() {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

var http=createObject();
/*----------------------------------------------------------*/
/* Login                                                    */
/*----------------------------------------------------------*/
var nocache=0;
function login() {
    document.getElementById('login_response').innerHTML="Loading...";
    var username = encodeURI(document.getElementById('username').value);
    var password = encodeURI(document.getElementById('password').value);
    nocache=Math.random();
    http.open('GET', './phpScripts/checklogin.php?username='+username+'&password='+password+'&nocache='+nocache);
    http.onreadystatechange=loginReply;
    http.send(null);
}
function loginReply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        if(response == 0) {//if login fails
            document.getElementById('login_response').innerHTML=
                'Login failed! Verify user and password';
        }
        else {//successful login
            window.location="index.php";
        }
    }
}
/*----------------------------------------------------------*/
/* Register                                                 */
/*----------------------------------------------------------*/
function register(myForm) {
    if(validate_register()) {
        myForm.submit();
    }
}
function validate_register() {
    var valid = true;
    var x=document.forms["registration_form"]["email"].value;
    var y;
    //verify email
    if(x==null || x=="") {
        valid = false;
        document.getElementById('email_error').innerHTML=
            'Error! Email must be provided';
    }
    else { 
        var atpos=x.indexOf("@");
        var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
            valid = false;
            document.getElementById('email_error').innerHTML=
            'Error! Not a valid e-mail address';
        }
        else document.getElementById('email_error').innerHTML="";
    }
    //verify name
    x=document.forms["registration_form"]["fName"].value;
    if(x==null || x=="") {
        valid = false;
        document.getElementById('fName_error').innerHTML=
            'Error! First name must be provided';
    }
    else document.getElementById('fName_error').innerHTML="";
    x=document.forms["registration_form"]["lName"].value;
    if(x==null || x=="") {
        valid = false;
        document.getElementById('lName_error').innerHTML=
            'Error! Last name must be provided';
    }
    else document.getElementById('lName_error').innerHTML="";
    //verify user type
    x=document.getElementById("professorType");
    y=document.getElementById("studentType");
    if(!x.checked && !y.checked) {
        valid = false;
        document.getElementById('uType_error').innerHTML=
            'Error! User type must be provided';
    }
    else document.getElementById('uType_error').innerHTML="";
    //verify password
    x=document.forms["registration_form"]["pass1"].value;
    y=document.forms["registration_form"]["pass2"].value;
    if(x==null || x=="" || y==null || y=="") {
        valid = false;
        document.getElementById('password_error').innerHTML=
            'Error! Password must be provided';
    }
    else if(x!=y) {
        valid = false;
        document.getElementById('password_error').innerHTML=
            'Error! Passwords do not match';
    }
    else document.getElementById('password_error').innerHTML="";
    //return verification status
    return valid; 
}
/*----------------------------------------------------------*/
/* Add Survey                                               */
/*----------------------------------------------------------*/
    function add_survey() {
    if(validate_add_survey()){
        var coursename = encodeURI(document.getElementById('courseName').value);
        var surveyname = encodeURI(document.getElementById('surveyName').value);
        nocache=Math.random();
        http.open('POST', './phpScripts/addSurvey.php?surveyName='+surveyname+'&courseName='+coursename+'&nocache='+nocache);
        http.onreadystatechange=add_survey_reply;
        http.send(null);
    }
}
function add_survey_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        var responseText;
        if(response == 0) {//if add survey fails
            responseText='Survey could not be added.';
        }
        else if (response == 1) {
            responseText='Survey already exists.';
        }
        else {//successful add
            responseText='Survey \'' + response + '\' added successfully.';
        }
        document.getElementById('add_survey_response').innerHTML=responseText;
        document.getElementById('courseName').value="";
        document.getElementById('surveyName').value="";
        populate_survey();
    }
}
function validate_add_survey() {
    var valid = true;
    var x=document.forms["add_survey_form"]["courseName"].value;
    if(x==null || x=="") {
        valid = false;
        document.getElementById('course_name_error').innerHTML=
            'Error! Course Name must be provided ';
    }
    else document.getElementById('course_name_error').innerHTML="";
    x=document.forms["add_survey_form"]["surveyName"].value;
    if(x==null || x=="") {
        valid = false;
        document.getElementById('survey_name_error').innerHTML=
            'Error! Survey Name must be provided ';
    }
    else document.getElementById('survey_name_error').innerHTML="";
    return valid;
}
/*----------------------------------------------------------*/
/* Survey Table                                             */
/*----------------------------------------------------------*/
function populate_survey() {
    nocache=Math.random();
    http.open('GET', './phpScripts/getSurveys.php?nocache='+nocache);
    http.onreadystatechange=populate_survey_reply;
    http.send(null);
}
function populate_survey_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        document.getElementById("surveyTable").innerHTML = response;
    }
}
/*----------------------------------------------------------*/
/* Student Survey Table                                     */
/*----------------------------------------------------------*/
function populate_student_survey() {
    nocache=Math.random();
    http.open('GET', './phpScripts/getStudSurveys.php?nocache='+nocache);
    http.onreadystatechange=populate_student_survey_reply;
    http.send(null);
}
function populate_student_survey_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        document.getElementById("studentSurveyTable").innerHTML = response;
    }
}
/*----------------------------------------------------------*/
/* Edit Survey Table                                        */
/*----------------------------------------------------------*/
function populate_edit_table (){
    nocache=Math.random();
    http.open('GET', './phpScripts/getQuestions.php?nocache='+nocache);
    http.onreadystatechange=populate_edit_reply;
    http.send(null);
}
function populate_edit_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        document.getElementById("edit_survey_table").innerHTML = response;
    }
}
/*----------------------------------------------------------*/
/* Edit Survey Table                                        */
/*----------------------------------------------------------*/
function populate_survey_table (){
    nocache=Math.random();
    http.open('GET', './phpScripts/populateSurvey.php?nocache='+nocache);
    http.onreadystatechange=populate_survey_table_reply;
    http.send(null);
}
function populate_survey_table_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        document.getElementById("take_survey_table").innerHTML = response;
    }
}
/*-----------------------------------------------------------*/
/* Add Question                                              */
/*-----------------------------------------------------------*/
function submit_survey(myForm) {
    var elem = myForm.elements;
    var filled = 0;
    var count = 0;
    for (var i=0;i<elem.length;i++) {
        if(elem[i].type=='radio'){
            count++;
            if(elem[i].checked) filled++;
        }
    }
    if(filled == count/5) {
        myForm.submit();
    }
    else {
       document.getElementById("take_survey_response").innerHTML = 
           "All questions must be completed before submitting survey.";
    }
}
/*-----------------------------------------------------------*/
/* Add Question                                              */
/*-----------------------------------------------------------*/
function add_question() {
    
    
    if(validate_add_question()) {
        var question = encodeURI(document.getElementById('question').value);
        nocache=Math.random();
        http.open('POST', './phpScripts/addQuestion.php?survey_ID=&question='+question+'&nocache='+nocache);
        http.onreadystatechange=add_question_reply;
        http.send(null);
    }
}
function add_question_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        var responseText;
        if(response == 0) {//if add survey fails
            responseText='Survey could not be added.';
        }
        else {//successful add
            responseText='Question added successfully.';
        }
        document.getElementById('add_question_response').innerHTML=responseText;
        document.getElementById('question').value="";
        populate_edit_table();
    }
}
function validate_add_question() {
    var valid = true;
    var x=document.forms["add_question_form"]["question"].value;
    if(x==null || x=="") {
        valid = false;
        document.getElementById('add_question_error').innerHTML=
            'Error! Question text must be provided.';
    }
    else document.getElementById('add_question_error').innerHTML="";
    return valid;
}
/*----------------------------------------------------------*/
/* Delete Question                                          */
/*----------------------------------------------------------*/
function delete_question(id) {
    var del=confirm('Do you wish to delete this question?');
    if(del){
        nocache=Math.random();
        http.open('GET', './phpScripts/deleteQuestion.php?id='+id+'&nocache'+nocache);
        http.onreadystatechange=delete_question_reply;
        http.send(null);
    }
}
function delete_question_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        document.getElementById("add_question_response").innerHTML = response;
        populate_edit_table();
    }
}
/*----------------------------------------------------------*/
/* Delete Survey                                            */
/*----------------------------------------------------------*/
function delete_survey(id) {
    var del=confirm('Do you wish to delete survey?');
    if(del){
        nocache=Math.random();
        http.open('GET', './phpScripts/deleteSurvey.php?id='+id+'&nocache='+nocache);
        http.onreadystatechange=delete_survey_reply;
        http.send(null);
    }
    else {
        
    }
}
function delete_survey_reply() {
    if(http.readyState == 4) {
        var response = http.responseText;
        document.getElementById('add_survey_response').innerHTML=response;
        populate_survey();
    }
}